package cn.tedu.oa.sys.controller;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.vo.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class LoginController {
    @RequestMapping("login")
    public String login(){
        return "login";
    }


    @LogAnnotation("登录操作")
    @RequestMapping("doLogin")
    @ResponseBody
    public JsonResult doLogin(String username, String password) {
        //1.对用户身份以及凭证信息进行封装
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //2. 获取Shiro框架中的主体对象
        Subject subject = SecurityUtils.getSubject();
        //3. 通过主体对象提交用户token信息

        try {
            subject.login(token);
        } catch (IncorrectCredentialsException e) {
            throw new ServiceException("用户名或密码错误");
        } catch (AuthenticationException e) {
            throw new AuthenticationException("认证失败");
        }
       
        return new JsonResult("login ok");
    }
}
